using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace OWED.Document
{
    public class AddTag: ChangeAtom
    {
        XmlNode child;

        //position of the child in the children list after the add
        int position;

        public AddTag(AbsolutePath addPath, XmlNode newChild, int childPosition)
        {
            path = addPath;
            child = newChild;
            position = childPosition;
        }

        public override void Apply(XmlDocument document)
        {
            XmlNode node = path.getNode(document);
            if (node.HasChildNodes)
            {
                XmlNode curChild = node.FirstChild ;
                for (int i = 1; i < position; i++)
                {
                    curChild = curChild.NextSibling;
                }
                node.InsertBefore(child, curChild);

            }
            else
            {
                node.AppendChild(child);
            }


        }
    }
}
